using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using Projecturo.ProjecturoClient.Command;
using System.Windows.Input;
using System.Windows;
using Projecturo.ProjecturoClient.Connection;
using Projecturo.Model;
using Neits.UserService;
using System.Threading;

namespace Projecturo.ProjecturoClient.ViewModel.Login
{
    public class LoginViewModel : WorkspaceViewModel
    {
        private LoginInformation loginInformation = new LoginInformation();

        private DelegateCommand loginCommand;
        private DelegateCommand cancelCommand;

        public string Username
        {
            get
            {
                return this.loginInformation.Username;
            }
            set
            {
                if (this.loginInformation.Username != value)
                {
                    this.loginInformation.Username = value;
                    OnPropertyChanged("Username");
                }
            }
        }

        public string Password
        {
            get
            {
                return this.loginInformation.Password;
            }
            set
            {
                if (this.loginInformation.Password != value)
                {
                    this.loginInformation.Password = value;
                    OnPropertyChanged("Password");
                }
            }
        }

        public ICommand LoginCommand
        {
            get
            {
                if(this.loginCommand == null)
                    this.loginCommand = new DelegateCommand(Login);
                return this.loginCommand;
            }
        }

        public ICommand CancelCommand
        {
            get
            {
                if(this.cancelCommand == null)
                    this.cancelCommand = new DelegateCommand(Cancel);
                return this.cancelCommand;
            }
        }

        private void Login()
        {
            DataContext context = new DataContext();
            IUser loggedInUser = Connector.Connectivity.Authentication.Login(new LoginInformation { Username=this.Username, Password=this.Password }, context);

            if (loggedInUser != null)
            {
                User user = loggedInUser as User;
                Thread.CurrentPrincipal = user;
                MainWindow window = new MainWindow();
                MainWindowViewModel windowViewModel = ViewModelManager.Instance.CreateViewModel<MainWindowViewModel>();
                windowViewModel.CloseRequested += delegate
                {
                    Application.Current.Shutdown();
                };
                window.DataContext = windowViewModel;
                window.Show();

                base.CloseCommand.Execute(null);
            }
        }

        private void Cancel()
        {
            Application.Current.Shutdown(0);
        }
    }
}
